const { User } = require('../../database/model/User')

// 导出
module.exports = async (req, res) =>
{
    // 接收客户端 传递过来的 信息
    const { userName, password, fullName, email, phoneNumber, departmentID = 1, avatarUrl = "https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png", userTypeID = 1, introduction, sex } = req.body
    // // 根据客户端传递过来的 用户名 查询 数据库 中 是否 存在这用户名
    const model = await User.findOne({ where: { userName }, attributes: { exclude: ['createdAt', 'updatedAt'] } })
    // // 判断
    if (model) {
        res.status(400).send({
            data: null,
            meta: {
                msg: "用户名已经存在！",
                status: 400
            }
        })
        return
    }
    const userData = {
        UserName: userName,
        Password: password,
        FullName: fullName,
        Email: email,
        PhoneNumber: phoneNumber,
        DepartmentID: departmentID,
        UserTypeID: userTypeID,
        Sex: sex,
        Introduction: introduction,
        Avatar: avatarUrl
    }
    // 创建用户
    const data = await User.create(userData)
    // 打印sql语句
    res.status(200).send({
        data,
        meta: {
            msg: "创建成功！",
            status: 201
        }
    })
}